home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / zkf102.zip / extend.txt < prev    next >
Text File  |  1995-01-04  |  7KB  |  148 lines

  1.                       C++ Language Extensions Added by ZK
  2.  
  3.  
  4. The c++ language is extended by zk to add a few minor features, such
  5. as new kinds of macros, which peacefully coexist with the standard
  6. "#define" macros, and which make it easier to implement a command
  7. shell without asking the user to type excess characters at the
  8. command line.  For example, to implement a typical command line
  9. command such as "copy *.txt c:\txt" using normal c++ macros would
  10. require excessive syntax such as "copy(*.txt,c:\\txt)", but by using
  11. the new kinds of macros provided by zk, it can be implemented such
  12. that the command the user types at the zk command line looks exactly
  13. the same as when typed at the operating system command line.
  14.  
  15. There are two new kinds of macros, #m and #m1.  They work similarly to
  16. #define, but with different syntax and features.  #define is unchanged.
  17.  
  18. #m macroname(arguments)
  19.    macro definition
  20. %
  21.  
  22. The % has to be the last character on the last line of the macro.  You
  23. don't need backslashes at the ends of lines as you do in #define macros.
  24. So you always need a % at the end of every #m macro, even if it's just
  25. one line, e.g. #m tmac(arg1,arg2) definition text %
  26.  
  27. The formal arguments are the same as for #define, except that you can
  28. also use an asterisk as the last formal argument, for the vararg feature.
  29.  
  30. The actual arguments can be the same as for a #define macro, except that
  31. the opening parenthesis has to touch the macro name, with no white space
  32. between them.  But instead of that you can use use a new syntax, which
  33. is improved and preferred, in which the arguments follow the macro name
  34. with no parentheses and are separated by white space.  For example, if
  35. the definition is  #m abc(x) x x %  then the call can be  abc(123)  or
  36. abc 123  but not  abc (123)  because that would result in  (123) (123)
  37. instead of  123 123.  See some of the examples later in this document
  38. (extend.txt) to clarify this.
  39.  
  40. The vararg feature uses an asterisk as the rightmost formal argument,
  41. and invokes that argument from within the macro by using the name vararg,
  42. or varargfirst or varargrest.  The cmds macro, shown later in this
  43. document, is an example to clarify this.  When the name vararg appears
  44. in a macro that uses this feature, it is replaced with all the text of
  45. all the arguments that take the place of the asterisk in the formals.
  46. The other two, varargfirst and varargrest, are for parsing that text,
  47. giving the first and rest of those arguments.  Recursion, as used in
  48. the cmds macro, makes such parsing work for indefinite lists.
  49.  
  50. When a macro uses the vararg feature and is called using the whitespace
  51. syntax (i.e., no parentheses), the actual argument list needs to be
  52. terminated to prevent it from extending to the end of the file.  Such
  53. termination is provided by a semicolon, which is consumed by the macro.
  54. Thus if you need a semicolon at the end of a statement implemented by
  55. such a macro, you should put it in the macro definition to avoid having
  56. to put two semicolons at the end of the macro call.  This only applies
  57. to macros that use the vararg feature.
  58.  
  59. #m1 is the same as #m except that the macro call has to be at the start
  60. of a C++ statement or declaration.  That reduces the probability that
  61. the macro name will conflict with names of variables, etc.  It's useful
  62. for defining commands to use at the command line.
  63.  
  64. The command line has an implied semicolon at the end, but it only applies
  65. in contexts where appropriate.  That is, only when you are typing in
  66. commands for immediate execution and a semicolon is expected;  Otherwise
  67. the command continues to the next line.  If you don't want an implied
  68. semicolon at that point, even though a semicolon is expected, you can
  69. continue the command to the next line explicitly with a backslash.  Try
  70. it yourself to understand which contexts are affected.  The whole purpose
  71. of this feature is to reduce your typing when typing simple commands, so
  72. it will be like an ordinary command shell for that purpose.
  73.  
  74. Here are some example macros, discussed further below:
  75.  
  76. #m1 sys(*) xsystem(#vararg); nop(); %
  77.  
  78. #m def1cmd(cmd)
  79.    #m1 cmd(*) sys cmd vararg; %%
  80. %
  81.  
  82. #m1 cmds(*)
  83.    #if #varargfirst[0]
  84.    def1cmd varargfirst
  85.    cmds varargrest;
  86.    #endif
  87. %
  88.  
  89. The sys macro use the vararg feature and calls a function named xsystem,
  90. which is an interface to the compiler vendor's system function, which
  91. executes an operating system command.  The nop() function does nothing,
  92. and its only purpose is to cause the last function on the command line
  93. to return void (to return nothing) because some types of return values
  94. are displayed automatically by zk, such as when you type in an expression
  95. and want to see the result.  The nop() causes the return value of xsystem
  96. to not be displayed.  It's the return value of system, and is normally
  97. not very useful in this context, and would confuse the output.
  98.  
  99. The def1cmd macro is used by the cmds macro to define one command.  The
  100. double %% is how a nested macro definition is terminated, to not confuse
  101. it with the single % terminating the outer macro.
  102.  
  103. The cmds macro calls itself recursively, once per argument.  See the
  104. discussion earlier in this document for an explanation of the vararg
  105. feature.  The #varargfirst[0] stringizes the first argument and takes
  106. its first character, and if that's 0, it means the stringize result is
  107. empty, which means there is no first argument, which means the whole
  108. argument list has been processed by the recursion.
  109.  
  110. In general, you can copy the style of the cmds macro to make other
  111. recursive macros that work the same way, even if you don't understand
  112. some of the more subtle points explained here.  Don't get confused by
  113. the stringizing.  The # attached to the start of the names vararg and
  114. varargfirst causes their results to be stringized, which means to
  115. enclose them in quotation marks and do any processing needed to make
  116. them legal string literals.  And don't get confused by the
  117. #varargfirst[0], which translates to ""[0] when the argument list is
  118. exhausted.  ""[0] is 0, because strings are terminated by a 0, and an
  119. empty string is terminated at [0], and that's how the cmds macro knows
  120. when to stop processing arguments.
  121.  
  122. Another added preprocessor feature is #H, which is a like #include, but
  123. only works with zk header files, *.zh, and which has slightly neater
  124. syntax.  If you have three header files named one.zh, two,zh, and
  125. three.zh, the normal #include syntax is three lines:
  126.  
  127.    #include "one.zh"
  128.    #include "two.zh"
  129.    #include "three.zh"
  130.  
  131. But the #H syntax is just one line:
  132.  
  133.    #H one two three
  134.  
  135. (with the .zh extensions omitted, because #H adds them internally.)
  136.  
  137. Also, using the #H feature, even just once in your program, turns on a
  138. flag which causes all header files used by your program, even those using
  139. #include, to only be processed once, regardless of how many times they
  140. are referenced.  This is normally desirable, because processing the same
  141. header file more than once is normally a waste of time, even if it has
  142. protection causing it to be skipped after the first time, because just
  143. reading it and skipping through it takes time, even if not very much.
  144.  
  145. But in general the #H feature is not very significant and you can safely
  146. ignore it, and use #include instead.
  147.  
  148.